package com.longnuoqi.mydoctor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.longnuoqi.mydoctor.activity.NewsContentActivity;
import com.longnuoqi.mydoctor.activity.NewsMoreActivity;
import com.longnuoqi.mydoctor.adapter.NewsGridAdapter;
import com.longnuoqi.mydoctor.adapter.NewsListViewAdapter;
import com.longnuoqi.mydoctor.entity.News;
import com.longnuoqi.mydoctor.utils.FileUtil;
import com.longnuoqi.mydoctor.utils.ViewPagerUtil;
import com.longnuoqi.mydoctor.widget.MyListView;
import com.yunhe.mydoctor.R;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by Lenovo on 2015/12/25.
 */
public class NewsTypeFragment extends Fragment {
    private int id;
    private String url;
    private String name;
    private ViewPager viewPager;
    private final static int[] pics = {R.mipmap.banner, R.mipmap.banner2}; //轮播页面图片

    private TextView hotMore;
    private TextView putMore;

    private GridView gridView;
    private NewsGridAdapter gridAdapter;

    private MyListView listView;
    private NewsListViewAdapter listViewAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_channel_layout, container, false);
        savedInstanceState = getArguments();
        id = savedInstanceState.getInt("id");
        url = "http://www.tngou.net/api/info/list?id=" + id + "&rows=8";
        name = "news_list_" + id + ".txt";
        initView(view);
        initViewPager(view);
        getData(view);
        return view;
    }

    private void initView(View view) {
        hotMore = (TextView) view.findViewById(R.id.news_hot_more);
        putMore = (TextView) view.findViewById(R.id.news_put_more);
        hotMore.setOnClickListener(onClickListener);
        putMore.setOnClickListener(onClickListener);
    }
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), NewsMoreActivity.class);
            intent.putExtra("id",id);
            startActivityForResult(intent,0);
        }
    };

    private void initViewPager(View view) {
        viewPager = (ViewPager) view.findViewById(R.id.news_type_viewpager);
        new ViewPagerUtil().viewPagerUtil(getContext(), view, viewPager, pics);
    }

    private void getData(final View view) {
        if (!FileUtil.fileIsExist(name)) {
            Log.e("==============", "news file:" + name + ":is not exist");
            RequestQueue queue = Volley.newRequestQueue(getActivity());
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    FileUtil.saveFile(s, name);
                    List<News> newsList = FileUtil.json4NewsItem(s);
                    initGridView(view, newsList);
                    initListView(view, newsList);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                }
            });
            queue.add(request);
        } else {
            Log.e("==============", "news file:" + name + ":exist");
            String s = FileUtil.readFile(name);
            List<News> newsList = FileUtil.json4NewsItem(s);
            initGridView(view, newsList);
            initListView(view, newsList);
        }
    }


    private void initGridView(View view, List<News> newsList) {
        List<News> list = new ArrayList<>();
        News news = null;
        for (int i = 0; i < 2; i++) {
            news = new News();
            news.setId(newsList.get(i).getId());
            news.setTitle(newsList.get(i).getTitle());
            news.setImg(newsList.get(i).getImg());
            list.add(news);
        }
        gridAdapter = new NewsGridAdapter(getActivity(), list);

        gridView = (GridView) view.findViewById(R.id.news_gridview);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showNews((int) view.findViewById(R.id.news_gridview_img).getTag());
            }
        });
    }

    private void initListView(View view, List<News> newsList) {
        List<News> list = new ArrayList<>();
        for (int i = 2; i < newsList.size(); i++) {
            list.add(newsList.get(i));
        }
        listView = (MyListView) view.findViewById(R.id.news_listview);
        listViewAdapter = new NewsListViewAdapter(getContext(), list);
        listView.setAdapter(listViewAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showNews((int) view.findViewById(R.id.news_item_title).getTag());
            }
        });
    }

    private void showNews(int id) {
        Intent intent = new Intent(getActivity(), NewsContentActivity.class);
        intent.putExtra("id", id);
        startActivityForResult(intent, 0);
    }

}
